﻿
using System;
using Script.Game;
using UnityEngine;
using ZYLFramework.Manage;
using ZYLFramework.UI;

namespace Game.UI
{
    public class PawnStatePanelCtrl:UICtrl
    {
        public PawnStatePanelView View;
        public PawnStatePanelModel Model;
        public override void Init()
        {
            View = BaseView as PawnStatePanelView;
            Model = BaseModel as PawnStatePanelModel;
            SceneVariants.RegisterMessageEvent(EventEnum.AddPawnState, AddPawnStatePanel);
            SceneVariants.RegisterMessageEvent(EventEnum.UpdatePawnState, UpdatePawnStatePanel);
            SceneVariants.RegisterMessageEvent(EventEnum.RemovePawnState, RemovePawnStatePanel);
            SceneVariants.RegisterMessageEvent(EventEnum.SetDebugInfo, SetDebugInfo);
        }

        /// <summary>
        /// 更新一个棋子面板
        /// </summary>
        /// <param name="objects"></param>
        public void UpdatePawnStatePanel(object[] objects)
        {
            var go = (GameObject)objects[0];
            if (Model.PawnMap.ContainsKey(go.transform))
            {
                SetPawnStatePanel(go); 
            }
        }
        
        /// <summary>
        /// 增加一个棋子面板
        /// </summary>
        /// <param name="objects"></param>
        public void AddPawnStatePanel(object[] objects)
        {
            var go = (GameObject)objects[0];
            StatePanel statePanel;
            if (Model.StatePanelPool.Count >= 1)
            {
                statePanel = Model.StatePanelPool[0];
                Model.StatePanelPool.RemoveAt(0);
            }
            else
            {
                statePanel = View.NewStatePanel();
            }
            Tool.Tool.SetVisible(statePanel.Go,true);
            statePanel.Go.GetComponent<FollowUI>().followTarget = go.transform;
            // var pos = Tool.Tool.WorldPos2UIPos(Camera.main, SceneVariants.GetUICamera(),go.transform.position,View.PawnStatePanel.GetComponent<RectTransform>());
            // Debug.Log($"{go.transform.position},{pos}");
            // statePanel.Go.GetComponent<RectTransform>().anchoredPosition  = pos;
            Model.PawnMap.Add(go.transform,statePanel);
            SetPawnStatePanel(go);
        }
        
        /// <summary>
        /// 删除一个棋子面板
        /// </summary>
        /// <param name="objects"></param>
        public void RemovePawnStatePanel(object[] objects)
        {
            var go = (GameObject)objects[0];
            if (Model.PawnMap.ContainsKey(go.transform))
            {
                var statePanel = Model.PawnMap[go.transform];
                Model.StatePanelPool.Add(statePanel);
                Tool.Tool.SetVisible(statePanel.Go,false);
                Model.PawnMap.Remove(go.transform);
            } 
        }
        
        /// <summary>
        /// 设置棋子面板
        /// </summary>
        /// <param name="pawn"></param>
        public void SetPawnStatePanel(GameObject pawn)
        {
            if (!Model.PawnMap.ContainsKey(pawn.transform))
            {
                return;
            } 
            var statePanel = Model.PawnMap[pawn.transform];
            var pawnState = pawn.GetComponent<PawnState>();
            statePanel.HpText.text = $"{pawnState.resource.Hp}/{pawnState.Property.Hp}";
        }
        
        void SetDebugInfo(object[] objects)
        {
            var str = (string)objects[0];
            View.Text.text = str;
        }
    }
}
